﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XH.SmartParking.IService;
using XH.SmartParking.Entities;

namespace XH.SmartParking.Service
{
    public class UserService : BaseService, IUserService
    {
        public UserService(DbContext context)
            : base(context)
        {

        }

        public bool CheckUserName(string userName)
        {
            return Query<SysUser>(u => u.UserName == userName).Count() > 0;
        }

        //根据搜索关键词进行数据检索
        public IEnumerable<SysUser> GetUsers(string key)
        {
            // 需要将搜索关键词加入判断。。。。。。
            return Set<SysUser>()
                  .Include(r => r.Roles)
                  .Where(m =>

                        string.IsNullOrEmpty(key) ||

                        m.UserName.Contains(key) ||

                        m.RealName.Contains(key) ||

                        m.Address.Contains(key)
                    );
        }

        //登录
        public SysUser Login(string username, string password)
        {
            var users = Set<SysUser>()
                 .Include(u => u.Roles)
                 .Where(u => u.UserName == username && u.Password == password)
                 .ToList();
            return users.FirstOrDefault();
        }
    }
}
